global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/df_inventor_weighted_wages.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"

capture noi {

qui do ${code_dir}/config/country_list.do 
qui do ${code_dir}/config/labeling_finalvars.do

********************************************************************************************************************************
*This script creates the supplement for table A 33 where we weight wages in clumns 3, 6 and 9 by the origin of patent inventors*
********************************************************************************************************************************
*Note: all this is for manufacturing only, and also only for our main deflator MP
*crucially we do NOT assign the homecountry differently here. Only its weight differs!


*import homecountries and their patent weights (needs to be run AFTER indepvars.do)
use ${dataset_dir}/indep_vars/bvd_year_country_multinational${weight_window}_tfacit1.dta, clear
keep BvD country_shr_1995
bys BvD : gen x = _n
keep if x == 1
drop x
compress
tempfile maxweight
save `maxweight'

*import firms
use ${dataset_dir}/dep_vars/bvd_year_list${weight_window}_${wtype}.dta, clear
keep BvD year
* Add wages
mmerge year using ${dataset_dir}/indep_vars/lswages_wide_MANUF.dta, unmatched(none)
mmerge year using ${dataset_dir}/indep_vars/hswages_wide_MANUF.dta, unmatched(none)
* note: no wage data in 2010 (kept for lagging dep var)

*Standard homecountry:
mmerge BvD using `maxweight', unmatched(none)

*import inventor patent weights
mmerge BvD using ${dataset_dir}/weights/bvdid_pat_weights_EPtr${weight_window}_1995_orbis2017_${wtype}.dta, unmatched(master)
gen missing_weights_1995=(_m==1)
drop _m
*import GDP as backup
cross using ${dataset_dir}/weights/shares_GDP_allctries_1995.dta
sort BvD year

gen dshare = .
foreach ctry of global countrylist1995 {
	*once again, set weights and supplement through gdp
	gen weight_`ctry' = share2_all_1995_`ctry'
	replace weight_`ctry' = share_GDP_`ctry' if weight_`ctry' == .
	*set the homecountryshare based on the largest patent weight, but assign the inventorweight
	replace dshare = weight_`ctry' if country_shr_1995 =="`ctry'"
}
drop share*

*the usual: creating weighted values, create weight nromalized foreign and home shares
foreach vv in lswMP hswMP {

	*m stands for manufacturing,
	local m m

	foreach ctry of global countrylist1995 {
		*check if variable exists (should, for all)
		confirm variable `vv'_`ctry'
		*create weighted values and ln version of that
		gen `vv'_wtd_`ctry' = weight_`ctry'* `vv'_`ctry'
		gen ln`vv'_wtd_`ctry' = weight_`ctry'* ln(`vv'_`ctry')
	}

	*create total and ln total across countries
	egen `vv'`m'_ALL_1995_wtd = rowtotal(`vv'_wtd_??), missing
	egen ln`vv'`m'_ALL_1995_wtd = rowtotal(ln`vv'_wtd_??), missing
	
	* Based on largest INVENTOR weight
	gen ln`vv'`m'_shr_home_1995_wtd = .
	gen `vv'`m'_shr_home_1995_wtd = .

	*Assign home share and log homeshare based on largest INVENTOR weight
	foreach ctry of global countrylist1995 {
		replace ln`vv'`m'_shr_home_1995_wtd = ln`vv'_wtd_`ctry' if country_shr_1995 =="`ctry'"
		replace `vv'`m'_shr_home_1995_wtd = `vv'_wtd_`ctry' if country_shr_1995 =="`ctry'"
	}

	*create foreign share and log foreign share as complements to that
	gen ln`vv'`m'_shr_foreign_1995_wtd = ln`vv'`m'_ALL_1995_wtd - ln`vv'`m'_shr_home_1995_wtd
	gen `vv'`m'_shr_foreign_1995_wtd = `vv'`m'_ALL_1995_wtd - `vv'`m'_shr_home_1995_wtd

	* Version 2: take home country wage and average foreign country wage (i.e. normalize)
	gen ln`vv'`m'_shr2_home_1995_wtd = ln`vv'`m'_shr_home_1995_wtd / dshare
	gen ln`vv'`m'_shr2_foreign_1995_wtd = ln`vv'`m'_shr_foreign_1995_wtd / (1-dshare)
	gen `vv'`m'_shr2_home_1995_wtd = `vv'`m'_shr_home_1995_wtd / dshare
	gen `vv'`m'_shr2_foreign_1995_wtd = `vv'`m'_shr_foreign_1995_wtd / (1-dshare)
	drop *`vv'_wtd_*
}

drop weight_??

drop lsw??_?? hsw??_??
cap drop ctry


*logs for the shares
foreach v in lsw hsw {
	foreach vr in shr_home shr_foreign shr2_home shr2_foreign {
		cap gen `v'MPm_`vr'_1995_a = ln(`v'MPm_`vr'_1995_wtd)
	}
}

foreach v in lsw hsw {
	gen `v'MPm_1995_a = ln(`v'MPm_ALL_1995_wtd )
	ren ln`v'MPm_ALL_1995_wtd ln`v'MPm_1995_a
}


*normalizing the shares
qui ds *_shr_home_1995_a
foreach vaar in `r(varlist)' {
	local vv = substr("`vaar'",1,strpos("`vaar'","_")-1)
	if "`vv'" == "lngdpgap" {
		continue
	}
	gen term_home_var = exp(`vv'_shr2_home_1995_a)/exp(`vv'_1995_a)*dshare
	gen term_foreign_var = exp(`vv'_shr2_foreign_1995_a)/exp(`vv'_1995_a)*(1-dshare)
	gen _term_home = term_home_var if year==1995
	gen _term_foreign = term_foreign_var if year==1995
	bys BvD : egen term_home_fixed = max(_term_home)
	bys BvD : egen term_foreign_fixed = max(_term_foreign)
	gen `vv'_shr4_home_1995_a = `vv'_shr2_home_1995_a*term_home_fixed
	gen `vv'_shr4_foreign_1995_a = `vv'_shr2_foreign_1995_a*term_foreign_fixed
	drop term_* _term_*
}


qui ds BvD year, not
foreach var in `r(varlist)' {
	ren `var' `var'iw
}



drop *MP*_shr_* *_shr2_* *home* ln*MP*

*labeling
labelingvarsfinal "lsw"
labelingvarsfinal "hsw"

save ${final_dir}/part_inventor_weighted_wages.dta, replace



}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat